package com.fjwt.gz.province.henan;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.oss.common.utils.StringUtils;
import com.fjwt.gz.core.interfaces.UserAssetService;
import com.fjwt.gz.pay.PayKit;
import com.fjwt.gz.zhyx.AESUtils;
import com.fjwt.gz.zhyx.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * 资产等级、绑卡消费  资产等级接口：郑州返回的是1~5
 */
@Slf4j
@Service
public class HenanUserAssetService implements UserAssetService {

    @Value("${isFormal.code}")
    private boolean isFormalCode;

    private final String API_KEY = "xyfMarket";
    private final String XXJ_KEY = "xyfMarketKey";

    /**
     * 字符串加密
     */
    private String getEncryptSign(String encStr, long timestamp) {
        return DigestUtils.md5DigestAsHex((encStr + XXJ_KEY + timestamp).getBytes());
    }

    /**
     * 资产等级
     */
    private Integer getAssetLevel(@NonNull String custNo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("idCardStr", custNo);
        log.info("客户等级接口加密前请求参数:{}", JSONUtil.toJsonStr(jsonObject));
        String custManageUrl = PayKit.CUST_MANAGE_URL;
        if (isFormalCode){
            custManageUrl = PayKit.CUST_MANAGE_URL_FORMAL;
        }
        String encryptStr = AESUtils.getInstance().encrypt(JSONUtil.toJsonStr(jsonObject));
        long timeStamp = System.currentTimeMillis();
        String authSign = this.getEncryptSign(encryptStr, timeStamp);
        log.info("客户等级接口请求地址：{}, 请求参数：{}, 加密串：{}", custManageUrl, encryptStr, authSign);
            HttpResponse response = HttpRequest.post(custManageUrl)
                    .header("Content-Type", MediaType.TEXT_PLAIN_VALUE)
                    .header("sign-type", SignUtils.SIGN_TYPE)
                    .header("auth-apikey", API_KEY)
                    .header("auth-sign", authSign)
                    .header("auth-timestamp", String.valueOf(timeStamp))
                    .header("Referer", "https://jrstyy.post183.net")
                    .timeout(15000)
                    .body(encryptStr)
                    .execute();
            log.info("客户等级response:{}", JSONUtil.toJsonStr(response));
            String body = response.body();
            log.info("客户等级返回的数据是：{}", JSONUtil.toJsonStr(body));
            JSONObject  reapJson = JSONObject.parseObject(body);
            if(reapJson!=null&& !StringUtils.isNullOrEmpty(reapJson.getString("code"))&&reapJson.getString("code").equals("0000")&& !StringUtils.isNullOrEmpty(reapJson.getString("data"))){
                JSONObject  dataJson = JSONObject.parseObject(reapJson.getString("data"));
                return dataJson.getInteger("vipFlag");
            }else {
               return null;
            }
    }

    @Override
    public Integer userAssetLevel(String cstmNo, int year, int lastMonth) {
        try {
            return getAssetLevel(cstmNo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("用户资产等级数据查询失败：{}", e);
        }
        return null;
    }

    @Override
    public JSONObject userAssetLevelList(List cstmNolist) {
        return null;
    }

    @Override
    public Integer userConsumption(String cstmNo, int year, int lastMonth) {
        return null;
    }

    @Override
    public JSONObject userConsumptionList(List cstmNolist) {
        return null;
    }

    @Override
    public Integer quarterUserAssetLevel(String cstmNo, int year, int lastMonth) {
        return null;
    }
}
